# Define here the models for your scraped items
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/items.html
import datetime

import scrapy

from components.custom_item import UpdateCustomItem
from utils.logs import log
from utils.tools import get_md5, del_redundant_blank_character, unescape, replace_str, get_info


class NetTaxPolicyDeclarationItem(UpdateCustomItem):
    project_id = scrapy.Field()
    source = scrapy.Field()
    province = scrapy.Field()
    city = scrapy.Field()
    county = scrapy.Field()
    declaration_type = scrapy.Field()
    page_view = scrapy.Field()
    project_name = scrapy.Field()
    enjoyment_subject = scrapy.Field()
    enjoyment_condition = scrapy.Field()
    doc_number = scrapy.Field()
    release_time = scrapy.Field()
    department = scrapy.Field()
    policy_theme = scrapy.Field()
    policy_basis = scrapy.Field()
    policy_sunnary = scrapy.Field()
    max_support_amount = scrapy.Field()
    content = scrapy.Field()
    source_url = scrapy.Field()
    application_area = scrapy.Field()
    administrative_level = scrapy.Field()
    policy_industry = scrapy.Field()
    declaration_start_time = scrapy.Field()
    application_deadline = scrapy.Field()
    application_materials = scrapy.Field()
    handling_process = scrapy.Field()
    charging_situation = scrapy.Field()
    processing_time = scrapy.Field()
    legal_time_limit = scrapy.Field()
    commitment_time_limit = scrapy.Field()
    declaration_address = scrapy.Field()
    consultant = scrapy.Field()
    hotline = scrapy.Field()
    complaints_hotline = scrapy.Field()
    valid = scrapy.Field()

    def format_date(self, date, old_format="", new_format="%Y-%m-%d"):
        if not date:
            return None

        if not old_format:
            regex = r"(\d+)"
            numbers = get_info(date, regex, allow_repeat=True)
            formats = ["%Y", "%m", "%d", "%H", "%M", "%S"]
            old_format = date
            for i, number in enumerate(numbers[:6]):
                if i == 0 and len(number) == 2:  # 年份可能是两位 用小%y
                    old_format = old_format.replace(
                        number, formats[i].lower(), 1
                    )  # 替换一次 '2017年11月30日 11:49' 防止替换11月时，替换11小时
                else:
                    old_format = old_format.replace(number, formats[i], 1)  # 替换一次
        try:
            date_obj = datetime.datetime.strptime(date, old_format)
            if "T" in date and "Z" in date:
                date_obj += datetime.timedelta(hours=8)
                date_str = date_obj.strftime("%Y-%m-%d %H:%M:%S")
                return date_str
            else:
                date_str = datetime.datetime.strftime(date_obj, new_format)
                return date_str
        except Exception as e:
            log.error("日期格式化出错，old_format = %s 不符合 %s 格式" % (old_format, date))
            pass

    def pre_to_db(self):
        self.project_name = self.delete_blank((self.project_name or "").strip())
        self.content = del_redundant_blank_character(self.content or "")
        self.policy_sunnary = del_redundant_blank_character(self.policy_sunnary or "")
        self.release_time = self.format_date((self.release_time or "").strip(), new_format="%Y-%m-%d") or None
        self.project_id = get_md5(
            self.province, self.city, self.project_name, self.release_time, self.source_url,
            self.policy_theme
        )
        self.declaration_start_time = (
            self.format_date((self.declaration_start_time or "").strip(), new_format="%Y-%m-%d") or None
        )
        self.application_deadline = (
            self.format_date((self.application_deadline or "").strip(), new_format="%Y-%m-%d") or None
        )
        self.processing_time = self.format_date((self.processing_time or "").strip(), new_format="%Y-%m-%d") or None
        self.legal_time_limit = self.format_date((self.legal_time_limit or "").strip(), new_format="%Y-%m-%d") or None
        self.commitment_time_limit = (
            self.format_date((self.commitment_time_limit or "").strip(), new_format="%Y-%m-%d") or None
        )
        self.max_support_amount = self.max_support_amount or None

    @staticmethod
    def delete_blank(text):
        text = unescape(text)
        text = replace_str(text, r"\u200b", "")
        text = replace_str(text, r"\s+", "")
        return text
